package org.wu.framework.easy.markdown.adapter;

import org.wu.framework.easy.markdown.endpoint.EasyMarkDownFieldPoint;
import org.wu.framework.easy.markdown.endpoint.MarkDownTable;
import org.wu.framework.easy.markdown.endpoint.MarkDownTableData;
import org.wu.framework.easy.markdown.stereotype.markdown.EasyMarkDownField;
import org.wu.framework.easy.markdown.utils.MarkdownUtil;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.wu.framework.core.utils.ObjectUtils;

import java.lang.reflect.Field;
import java.util.*;

/**
 * markdown 适配器
 */
public class ExcelMarkDownAdapter {
    /**
     * 导出markdown
     *
     * @param markdownSourceBeanList 原始数据
     * @return markdown 字符串对应的字节流
     */
    public byte[] exportMarkDown(Collection<?> markdownSourceBeanList) {
        if (ObjectUtils.isEmpty(markdownSourceBeanList)) {
            throw new IllegalArgumentException("导出对象为空");
        }

        List<?> markdownBeanList = markdownSourceBeanList.stream().filter(Objects::nonNull).toList();
        Object markdownFirstBean = markdownBeanList.stream().findFirst().get();
        Class<?> markdownFirstBeanClass = markdownFirstBean.getClass();

        // 获取所有头
        List<EasyMarkDownFieldPoint> easyMarkDownFieldPointList = findEasyMarkDownFieldPointList(markdownFirstBeanClass);
        MarkDownTable markDownTable = new MarkDownTable();

        // 头数据
        List<MarkDownTableData> markDownTableHeaderDataList = easyMarkDownFieldPointList.stream().map(easyMarkDownFieldPoint -> {
            MarkDownTableData markDownTableData = new MarkDownTableData();
            markDownTableData.setValue(easyMarkDownFieldPoint.getName());
            return markDownTableData;
        }).toList();

        markDownTable.setMarkDownTableHeader(markDownTableHeaderDataList);
        for (Object markdownBean : markdownBeanList) {
            // 填充行数据
        }
        for (EasyMarkDownFieldPoint easyMarkDownFieldPoint : easyMarkDownFieldPointList) {

        }
        return MarkdownUtil.markdownTableToByte(markDownTable);
    }

    /**
     * 解析对象 中字段注解
     *
     * @param markdownFirstBeanClass 对象class
     * @return List<EasyMarkDownFieldPoint>
     */
    private List<EasyMarkDownFieldPoint> findEasyMarkDownFieldPointList(Class<?> markdownFirstBeanClass) {
        List<EasyMarkDownFieldPoint> easyMarkDownFieldPointList = new ArrayList<>();

        if (Map.class.isAssignableFrom(markdownFirstBeanClass)) {
            // TODO
        } else {
            for (Field declaredField : markdownFirstBeanClass.getDeclaredFields()) {
                EasyMarkDownFieldPoint easyMarkDownFieldPoint = new EasyMarkDownFieldPoint();
                easyMarkDownFieldPoint.setField(declaredField);

                String name = declaredField.getName();
                boolean basicDataType = true;
                EasyMarkDownField easyMarkDownField = AnnotatedElementUtils.findMergedAnnotation(declaredField, EasyMarkDownField.class);

                if (easyMarkDownField != null) {
                    name = easyMarkDownField.name();
                    basicDataType = easyMarkDownField.isBasicDataType();
                    if (!basicDataType) {
                        // 非基本数据类型
                        List<EasyMarkDownFieldPoint> markDownFieldPointList = findEasyMarkDownFieldPointList(declaredField.getClass());
                        if (Map.class.isAssignableFrom(declaredField.getClass())) {
                            easyMarkDownFieldPoint.setMapFiled(true);
                        } else if (Collection.class.isAssignableFrom(declaredField.getClass())) {
                            easyMarkDownFieldPoint.setCollectionFiled(true);
                        }
                        easyMarkDownFieldPoint.setMarkDownBeanFiledPointList(markDownFieldPointList);
                    }
                } else {

                }
                easyMarkDownFieldPoint.setName(name);
                easyMarkDownFieldPoint.setBasicDataType(basicDataType);

                easyMarkDownFieldPointList.add(easyMarkDownFieldPoint);
            }
        }

        return easyMarkDownFieldPointList;
    }

    /**
     * 获取 字段名
     *
     * @param declaredField 字段
     * @return 字段名
     */
    private String findEasyMarkDownFieldName(Field declaredField) {
        EasyMarkDownField easyMarkDownField = AnnotatedElementUtils.findMergedAnnotation(declaredField, EasyMarkDownField.class);
        if (easyMarkDownField == null) {
            return declaredField.getName();
        } else {
            return easyMarkDownField.name();
        }

    }


}
